package com.bluefox.system.quartz;

import org.quartz.*;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.Date;

/**
 * @author 孙峰
 * @Description: TODO
 * @date 2020/12/1 14:18
 */
public class Demo {
    public static void main(String[] args) throws Exception {

        JobDetail jobDetail = JobBuilder.newJob(DemoJob.class).withIdentity("Job01").build();

        // 获取任务的名称
        System.out.println(jobDetail.getKey().getName());
        // 任务组的名称   如果没有指定  使用默认DEFAULT
        System.out.println(jobDetail.getKey().getGroup());
        // 获取任务执行类的名称
        System.out.println(jobDetail.getJobClass().getName());

        jobDetail.getJobDataMap().put("message","jobDetail");

        jobDetail.getJobDataMap().put("count",0);

        // 两种常用的
        SimpleScheduleBuilder simpleScheduleBuilder = SimpleScheduleBuilder.repeatSecondlyForever(5);
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");

        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("Trigger01")
                .startNow()
                .withSchedule(cronScheduleBuilder)
                .build();
        cronTrigger.getJobDataMap().put("message","cronTrigger");


        //重置启动时间
        ((CronTriggerImpl) cronTrigger).setStartTime(new Date());


        SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();

        factoryBean.afterPropertiesSet();
        Scheduler scheduler = factoryBean.getScheduler();
         scheduler.start();
        //调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail, cronTrigger);


    }
}
